﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yxinmt.Interface;
using Yxinmt.SmarterStreaming;
using Yxinmt.Utility;

namespace Yxinmt.Controls
{
    public class PictureBoxPlayer
    {
        public System.Windows.Forms.PictureBox PictureBox { get; set; }

        public Dictionary<string, object> _Options { get; set; }

        public IWindow Window { get; set; }

        public static SmarterStreaming.SmartPlayer SmartPlayer { get; set; }

        public PictureBoxPlayer(Dictionary<string, object> options, IWindow window)
        {
            _Options = options;
            Window = window;
        }

        public void Play(string fileName)
        {
            try
            {
                SmartPlayer = StreamingMediaManager.StartPlay(this.Window.Window, PictureBox, fileName, this.SetStatus);
            }
            catch (Exception ex)
            {
                Stop();
                this.Window.Alert( "PictureBoxPlayer", "Play", ex);
            }
        }

        public void InitSetStatus(IControl ctl, string name)
        {
            SetStatus = (s) =>
            {
                this.Window.TaskRun("SetStatus", false, () =>
                {
                    this.Window.Window.Dispatcher.Invoke(() =>
                    {
                        try
                        {
                            if (ctl == null) Console.WriteLine(s);
                            else ctl.SetValue(string.IsNullOrEmpty(s) ? name : name + s);
                        }
                        catch
                        {
                        }
                    });
                });
            };
        }

        public Action<string> SetStatus { get; set; }
        public Action<bool> SetFullScreen { get; set; }

        System.Windows.Forms.Integration.WindowsFormsHost _WindowsFormsHost { get; set; }

        public void InitControl(System.Windows.Forms.Integration.WindowsFormsHost windowsFormsHost)
        {
            _WindowsFormsHost = windowsFormsHost;

            PictureBox = new System.Windows.Forms.PictureBox();

            _Panel = new System.Windows.Forms.Panel();
            _Panel.Dock = System.Windows.Forms.DockStyle.Fill;
            windowsFormsHost.Child = _Panel;

            PictureBox.Name = _Options.GetStringValue("Name");
            if (!IsFullScreen)
            {
                PictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
                _Panel.Controls.Add(PictureBox);
            }
            else SetPictureBoxFullSreen();

            PictureBox.DoubleClick += PictureBox_DoubleClick;
        }

        public void SetPictureBoxFullSreen()
        {
            IsFullScreen = true;
            PictureBox.Left = 0;
            PictureBox.Top = 0;
            PictureBox.Width = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
            PictureBox.Height = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;
            Win32Api.SetParent(PictureBox.Handle, IntPtr.Zero);
        }

        System.Windows.Forms.Panel _Panel { get; set; }

        void PictureBox_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                if (SmartPlayer == null || PictureBox == null || _Panel == null) return;

                if (IsFullScreen)
                {
                    ClosePictureBoxFullScreen();
                    if (SmartPlayer != null) SmartPlayer.SetSize(PictureBox.Width, PictureBox.Height);
                }
                else
                {
                    PictureBox.Dock = System.Windows.Forms.DockStyle.None;
                    SetPictureBoxFullSreen();

                    if (SmartPlayer != null) SmartPlayer.SetSize(PictureBox.Width, PictureBox.Height);
                }

                if (SetFullScreen != null) SetFullScreen(IsFullScreen);
            }
            catch (Exception ex)
            {
                LoggerProxy.Exception("PictureBoxPlayer", "PictureBox_DoubleClick", ex);
            }
        }

        public void ClosePictureBoxFullScreen()
        {
            IsFullScreen = false;
            PictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
            Win32Api.SetParent(PictureBox.Handle, _Panel.Handle);
        }

        public bool IsFullScreen { get; set; }

        public void Stop()
        {
            try
            {
                StreamingMediaManager.StopPlay(SmartPlayer);
                SmartPlayer = null;
                if (IsFullScreen) ClosePictureBoxFullScreen();
            }
            catch (Exception ex)
            {
                LoggerProxy.Exception("PictureBoxPlayer", "Stop", ex);
            }
        }

        public void SetBackClose()
        {
            _Panel.Dock = System.Windows.Forms.DockStyle.Fill;
            _Panel.Left = 0;
            _Panel.Top = 0;
            Win32Api.SetParent(_Panel.Handle, _WindowsFormsHost.Handle);
        }
    }
}
